package com.my.study.java.consumer;

import com.my.study.common.constant.TopicConstants;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author Carlos
 * @version 1.0
 * @Description 优化的多线程操作消息处理
 * @date 2021/7/25 13:21
 **/
public class PositionConsumerPlus {

    private static final String BROKER_LIST = "47.98.50.37:9092";
    private static final String GROUP_ID = "group.demo";

    private static final Map<TopicPartition, OffsetAndMetadata> offsets = new ConcurrentHashMap<>();


    private static Properties initConfig() {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BROKER_LIST);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        props.put(ConsumerConfig.CLIENT_ID_CONFIG, "consumer.id.demo");

        return props;
    }


    public static void main(String[] args) {
        Properties prop = initConfig();

        KafkaConsumerThread consumerThread = new KafkaConsumerThread(prop, TopicConstants.TOPIC02,
                Runtime.getRuntime().availableProcessors());

        consumerThread.start();

    }



    static class KafkaConsumerThread extends Thread {
        private KafkaConsumer<String, String> kafkaConsumer;
        private ExecutorService executorService;
        private int threadNum;


        public KafkaConsumerThread(Properties prop, String topic, int threadNum) {
            this.kafkaConsumer = new KafkaConsumer<String, String>(prop);
            this.kafkaConsumer.subscribe(Collections.singleton(topic));
            this.threadNum = threadNum;
            executorService = new ThreadPoolExecutor(threadNum, threadNum,
                    0L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000),
                    new ThreadPoolExecutor.CallerRunsPolicy());
        }

        @Override
        public void run() {
            try {
                while (true) {
                    ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofMillis(100));

                    if (!records.isEmpty()) {
                        executorService.submit(new RecordsHandle(records));

                        // 位移提交
                        synchronized (offsets) {
                            if (!offsets.isEmpty()) {
                                kafkaConsumer.commitSync(offsets);
                                offsets.clear();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                kafkaConsumer.close();
            }
        }
    }

    static class RecordsHandle implements Runnable {
        private final ConsumerRecords<String, String> records;

        public RecordsHandle(ConsumerRecords<String, String> records) {
            this.records = records;
        }

        @Override
        public void run() {
            Set<TopicPartition> partitions = records.partitions();
            for (TopicPartition tp : partitions) {
                List<ConsumerRecord<String, String>> tpRecords = this.records.records(tp);
                // TODO 处理消息

                // 获取此次最大的消费位移
                long lastConsumerOffset = tpRecords.get(tpRecords.size() - 1).offset();
                synchronized (offsets) {
                    if (!offsets.containsKey(tp)) {
                        offsets.put(tp, new OffsetAndMetadata(lastConsumerOffset + 1));
                    }
                    else {
                        long position = offsets.get(tp).offset();
                        if (position < lastConsumerOffset + 1) {
                            offsets.put(tp, new OffsetAndMetadata(lastConsumerOffset + 1));
                        }
                    }
                }
            }
        }
    }

}
